<template>
  <div class="app-container calendar-list-container" id="news" >
    <div class="create">
      <el-button class="create-btn"
                 @click="handleCreate"
                 type="primary"
                 icon="el-icon-edit"
                 style="margin-bottom: 10px"
      >发布新闻</el-button>
    </div>

    <el-table :data="news"
              v-loading.fullscreen="isLoading"
              element-loading-background="rgba(0, 0, 0, 0.6)"
              border
              fit
              highlight-current-row
              style="width: 100%"
    >
      <el-table-column align="center" label="发布时间"  max-width="300px" min-width="150">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span>{{scope.row.time | parseTime('{y}-{m}-{d} {h}:{i}')}}</span>
        </template>
      </el-table-column>

      <el-table-column min-width="450px" label="标题">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column min-width="300px" label="简述">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{ scope.row.description }}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->

      <el-table-column align="center" label="操作" width="250">
        <template slot-scope="scope">
          <el-button type="success"  @click="handleCheck(scope.row)" size="small">详情</el-button>
          <el-button type="primary"  @click="handleUpdate(scope.row)" size="small">编辑</el-button>
          <el-button type="danger" @click='handleDelete(scope.row)' size="small" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <check-dialog :checkTemp="checkTemp"
                  :dialogCheckVisible="dialogCheckVisible"
                  @close="handleCloseCheck"
    ></check-dialog>

    <update-dialog :updateTemp="updateTemp"
                   :dialogUpdateFormVisible="dialogUpdateFormVisible"
                   :initContent = "initContent"
                   @cancel="cancelEdit"
                   @update="updateNews"
                   ref="update"
    ></update-dialog>

    <create-dialog :createTemp="createTemp"
                   :dialogCreateFormVisible="dialogCreateFormVisible"
                   :initContent = "initContent"
                   @cancel="cancelCreate"
                   @create="createNews"
    ></create-dialog>
  </div>
</template>

<script>
  import { getNews, createNews, updateNews, deleteNews } from '@/api/news'
  import { parseTime } from '@/utils'
  import UpdateDialog from './components/UpdateDialog'
  import CreateDialog from './components/CreateDialog'
  import CheckDialog from './components/CheckDialog'

  export default {
    components: {
      UpdateDialog,
      CreateDialog,
      CheckDialog
    },
    data() {
      return {
        news: null,
        isLoading: false,
        deleteLoading: false,
        dialogUpdateFormVisible: false,
        dialogCreateFormVisible: false,
        dialogCheckVisible: false,
        updateTemp: {},
        createTemp: {},
        deleteTemp: {},
        checkTemp: {},
        initContent: false,
        admin_id: null,
        token: null
      }
    },
    filters: {
      parseTime
    },
    created() {
      this.admin_id = this.$store.state.user.admin_id
      this.token = this.$store.state.user.token
      this.getNews()
    },
    methods: {
      getNews() {
        this.listLoading = true
        getNews(this.admin_id, this.token).then(res => {
          const items = res.data
          this.news = items
          this.isLoading = false
        })
      },
      handleCreate() {
        this.createTemp = { content: '请输入新闻内容', img: '' }
        this.initContent = true
        this.dialogCreateFormVisible = true
      },
      cancelCreate(boolean) {
        this.dialogCreateFormVisible = boolean
        this.initContent = false
      },
      createNews(val) {
        var isVali = this.draftForm(this.createTemp)
        if (isVali) {
          var formData = new FormData()
          formData.append('admin_id', this.admin_id)
          formData.append('token', this.token)
          formData.append('title', this.createTemp.title)
          formData.append('content', this.createTemp.content)
          formData.append('img', val)
          this.isLoading = true
          createNews(formData).then(res => {
            this.isLoading = false
            this.dialogCreateFormVisible = false
            this.initContent = false
            this.$notify({
              title: '成功',
              message: '发布成功',
              type: 'success',
              duration: 2000
            })
            this.getNews()
          })
        }
      },
      handleCheck(row) {
        this.checkTemp = Object.assign({}, row) // copy obj
        this.dialogCheckVisible = true
      },
      handleCloseCheck(boolean) {
        this.checkTemp = {}
        this.dialogCheckVisible = boolean
      },
      handleUpdate(row) {
        this.updateTemp = Object.assign({}, row) // copy obj
        this.dialogUpdateFormVisible = true
        this.initContent = true
      },
      cancelEdit(boolean) {
        this.dialogUpdateFormVisible = boolean
        this.initContent = false
      },
      updateNews(val) {
        // console.log('val',val)
        var isVali = this.draftForm(this.updateTemp)
        if (isVali) {
          var formData = new FormData()
          formData.append('admin_id', this.admin_id)
          formData.append('token', this.token)
          formData.append('title', this.updateTemp.title)
          formData.append('content', this.updateTemp.content)
          formData.append('news_id', this.updateTemp.id)
          formData.append('img', val)
          this.isLoading = true
          updateNews(formData).then(res => {
            this.isLoading = false
            this.dialogUpdateFormVisible = false
            this.initContent = false
            this.$notify({
              title: '成功',
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
            this.getNews()
          })
        }
      },
      handleDelete(row) {
        this.$confirm('此操作将永久删除该新闻, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteTemp = Object.assign({}, row)
          this.isLoading = true
          deleteNews(this.admin_id, this.token, this.deleteTemp.id).then(res => {
            this.isLoading = false
            this.dialogUpdateFormVisible = false
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getNews()
          })
        }).catch(() => {
          this.$notify({
            title: '取消',
            message: '已取消删除',
            type: 'info',
            duration: 2000
          })
        })
      },
      draftForm(data) {
        if (data.content && data.title && data.content !== '请输入新闻内容') {
          return true
        } else {
          // console.log('false')
          this.$message({
            message: '请填写必要的新闻标题及新闻内容',
            type: 'error'
          })
          return false
        }
      }
    }
  }
</script>

<style scoped>
  .edit-input {
    padding-right: 100px;
  }
  .cancel-btn {
    position: absolute;
    right: 15px;
    top: 10px;
  }
</style>
